<template>
  <div class="bottom_layout">
    <el-row type="flex" class="row-bg" justify="center">
      <el-col :xl="6" :lg="7" class="left">
        <h2>欢迎使用此管理系统</h2>
        <span>Just log in, you can enjoy more services, interact with me here, and publish your opinions</span>

      </el-col>

      <el-col :span="1" class="center">
        <el-divider direction="vertical"></el-divider>
      </el-col>
      <el-col :xl="6" :lg="7" class="right">
        <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="80px">
          <el-form-item label="用户名" prop="username" style="width: 380px;">
            <el-input v-model="loginForm.username" placeholder="Username" clearable></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" style="width: 380px;">
            <el-input v-model="loginForm.password" type="password" placeholder="Password" show-password></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="code" style="width: 380px;">
            <el-input v-model="loginForm.code" style="width: 172px; float: left" maxlength="5"></el-input>
            <el-image :src="captchaImg" class="captchaImg" @click="getCaptcha"></el-image>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('loginForm')" :loading="loading">登录</el-button>
            <el-button @click="resetForm('loginForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>


</template>

<script>
import {getCaptcha} from "@/api/login";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '03252416',
        code: '',
        token: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
          {min: 5, max: 5, message: '长度为 5 个字符', trigger: 'blur'}
        ],
      },
      captchaImg: null,
      passwordType: 'password',
      loading: false
    };
  },
  methods: {
    //提交表单
    submitForm(formName) {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('login', this.loginForm).then(() => {
            this.$router.replace("/").catch(err => {
            }); //登录成功之后重定向到首页
            this.loading = false
          }).catch(err => {
            this.getCaptcha()
            this.loading = false
            // this.$message.error(err); //登录失败提示错误
          });
        } else {
          return false;
        }
      })
    },
    resetForm() {
      this.loginForm.username = '';
      this.loginForm.password = '';
      this.loginForm.code = '';
      this.loginForm.token = '';
    },
    //获取验证码
    getCaptcha() {
      getCaptcha().then(res => {
        this.loginForm.token = res.data.token
        this.captchaImg = res.data.captchaImg
      })
    },
  },
  created() {
    this.getCaptcha()
  }
}
</script>

<style scoped>
.bottom_layout {
  height: 100%;
  background-image: linear-gradient(to bottom right, #bdbef8, #b9a3fd);
  display: flex;
  align-items: center;
  text-align: center;
  justify-content: center;
}

.left {
  background: #fff;
}

.left > h2 {
  font: 900 25px '';
  margin: 50px 40px 40px;
}

.left > span {
  display: block;
  color: #999;
  font-size: 14px;
  margin: 40px;
}

.center > .el-divider {
  height: 100%;
}

.right {
  margin-top: 20px;
}

.captchaImg {
  float: left;
  margin-left: 8px;
  border-radius: 4px;
}
</style>
